//
//  AllCommentVC.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/30.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "AllCommentVC.h"
#import "AllCommentTableViewCell.h"
#import "customTextField.h"
#import "loginVC.h"
@interface AllCommentVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)UIView *underView;
@property (nonatomic,strong)customTextField *editTF;
@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation AllCommentVC
{
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"所有评论";
    [self.view addSubview:self.tableView];
    [self addUnderView];
    [self getData];
    
}
-(void)getData{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"tid"] = @(self.model.Id);
    params[@"alive"] = @"off";
    [HTTPManagers postCommentWithURL:@"comment" params:params success:^(id json) {
        
        self.dataArr = [json objectForKey:@"message"];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
-(void)addUnderView{
    _underView = [[UIView alloc]initWithFrame:CGRectMake(0, _tableView.bottom, SCREEN_WIDTH, FTab)];
    
    _editTF = [[customTextField alloc]initWithFrame:CGRectMake(20, 10, SCREEN_WIDTH-60-40, 29) leftImagePath:@"bianji" PlaceHolder:@"编辑..."];
    [_underView addSubview:_editTF];
    
    UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    sendBtn.frame = CGRectMake(SCREEN_WIDTH-60, 10, 40, 29);
    [sendBtn setTitle:@"发表" forState:UIControlStateNormal];
    sendBtn.titleLabel.font = FONT(14);
    [sendBtn setTitleColor:RGBCOLOR(0x25, 0x43, 0x7f) forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];
    [_underView addSubview:sendBtn];
    
    [self.view addSubview:_underView];
}
-(void)sendAction{
    if (_editTF.left<5) {
        [SVProgressHUD showErrorWithStatus:@"回复内容(内容长度最低为5)" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"tid"] = @(self.model.Id);
    params[@"uid"] = @"";
    params[@"content"] = _editTF.text;
    [HTTPManagers postCommentAddWithURL:@"add" params:params success:^(id json) {
        if ([json[@"code"]integerValue]==1) {
            loginVC *vc = [loginVC new];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [SVProgressHUD showInfoWithStatus:@"评论成功" maskType:SVProgressHUDMaskTypeBlack];
            [_editTF resignFirstResponder];
            [self getData];
        }
        
    } failure:^(NSError *error) {
        
    }];


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataArr!=nil &&self.dataArr!=[NSNull null]) {
        return self.dataArr.count;
    }
    return 0;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AllCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AllCommentTableViewCell class]) forIndexPath:indexPath];
    
    if (self.dataArr.count>0) {
        NSDictionary *dict = self.dataArr[indexPath.row];
        NSInteger timeSp = [dict[@"Creation"]integerValue];
        NSString *time = [YXComUtil changeTimeToTimeStr:timeSp formate:@"yyyy.MM.dd"];
        [cell customName:dict[@"UserName"] time:time content:dict[@"Content"]];
    }
    return cell;
    
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-FTab) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([AllCommentTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([AllCommentTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            [_tableView.mj_header endRefreshing];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            page++;
           
            [_tableView.mj_footer endRefreshing];
        }];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
